package T7_26;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

public class Test3 {
    public static void main(String[] args) throws Exception {
        //创建FileReader对象，指定要读取的文件
        FileReader fileReader = new FileReader("src\\T7_26\\account2.json");
        //定义一个int类型的变量length，并令其等于零
        int length = 0;
        //定义一个stringbulid类型的变量存放读取的字符
        StringBuilder stringBuilder = new StringBuilder();

        //通过循环来判断是否读取到文件的末尾
        while ((length = fileReader.read()) != -1) {
            //将读取到的字符加入stringbulider
            stringBuilder.append((char) length);
        }

        //关闭流
        fileReader.close();
        System.out.println(stringBuilder);


        /*
        //将读取的json字符串序列化并赋值给json项目,然后从该json项目中get出需要的json数组并遍历赋值给类数组
        JSONObject jsonObject = JSON.parseObject(String.valueOf(stringBuilder));
        JSONArray jsonArray = jsonObject.getJSONArray("user");
        Account2_user[] users = new Account2_user[jsonArray.size()];

        for (int i=0;i < jsonArray.size();i++){
            JSONObject jsonObject2 = jsonArray.getJSONObject(i);
            users[i].setAccount(jsonObject2.getString("account"));
            users[i].setPassword(jsonObject2.getString("password"));
            users[i].setPs(jsonObject2.getString("ps"));
        }

        //测试用循环
        for (int i=0;i < users.length;i++){
            System.out.println(users[i].getAccount());
            System.out.println(users[i].getPassword());
            System.out.println(users[i].getPs());
        }

         */

        /*List<HashMap> users = JSON.parseArray(String.valueOf(stringBuilder), HashMap.class);
        for (int i = 0; i < users.size(); i++) {
            System.out.println(users.get(i).get("account"));
            System.out.println(users.get(i).get("password"));
            System.out.println(users.get(i).get("ps"));
        }

         */

        /* 留着观察
        // 2.使用JSONObject对象中的信息
		JSONArray jsonArray = jsonObject.getJSONArray("comments");
		System.out.println("声明的json数组信息:" + jsonArray);

		// 3.使用增强型循环遍历输出数组中的信息
		System.out.println("循环输出数组中的对象信息");
		for (Object object : jsonArray) {
			JSONObject obj = (JSONObject) object;
			System.out.println("声明的json对象信息:" + obj);
			System.out.println("name=" + obj.get("name") + ",age=" + obj.get("age"));
		}
         */

        //list->json测试
        //List<Account2_user> accountList = new ArrayList<Account2_user>();
        //json->list测试

        List<Account2_user> accountList = JSON.parseArray(String.valueOf(stringBuilder), Account2_user.class);
        for (Account2_user account2_user:accountList){
            System.out.println(account2_user.getAccount());
            System.out.println(account2_user.getPassword());
            System.out.println(account2_user.getPs());
        }
        //Account2_user account2_user = new Account2_user();
        System.out.println("-----------------------------------------");
        //尝试写入修改


        for (Account2_user account2_user:accountList){
            if (account2_user.getAccount().equals("user2")){
                account2_user.setPs("找到的元素在这");
            }
        }

        //写入数据
        char[] endchar = JSON.toJSONString(accountList).toCharArray();

        StringBuilder endstring = new StringBuilder();

        for(int i=0;i < endchar.length;i++){
            endstring.append(endchar[i]);
            switch (endchar[i]) {
                case '{':
                case '[':
                    endstring.append("\n");
                    break;
                case '}':
                case ']':
                    endstring.insert(endstring.length()-1,"\n");
                    //先删除现在的"}",再加上\n,再加回来"}"(方法已废弃)
                    //endstring.deleteCharAt(endstring.length()-1);
                    //endstring.append("\n");
                    //endstring.append(endchar[i]);
                    break;
                case ',':
                    endstring.append("\n");
                    break;
                default:break;
            }
        }

        //写入格式化后的数据
        FileWriter fileWriter = new FileWriter("src\\T7_26\\account2.json");

        fileWriter.write(String.valueOf(endstring));

        fileWriter.close();

        System.out.println("已写入");



    }
}
